package com.smartscreen.attendancemanage.service.impl;

import com.smartscreen.attendancemanage.dto.FaceRecognitionCheckInRequest;
import com.smartscreen.attendancemanage.dto.ManualCheckInRequest;
import com.smartscreen.attendancemanage.dto.StudentInfoDTO;
import com.smartscreen.attendancemanage.entity.AttendanceRecord;
import com.smartscreen.attendancemanage.mapper.AttendanceRecordMapper;
import com.smartscreen.attendancemanage.service.AttendanceService;
import com.smartscreen.attendancemanage.service.StudentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class AttendanceServiceImpl implements AttendanceService {
    
    @Autowired
    private AttendanceRecordMapper attendanceRecordMapper;
    
    @Autowired
    private StudentInfoService studentInfoService;

    @Override
    public String manualCheckIn(ManualCheckInRequest request) {
        String recordId = "rec_" + UUID.randomUUID().toString().substring(0, 8);
        
        AttendanceRecord record = new AttendanceRecord();
        record.setRecordId(recordId);
        record.setStudentId(request.getStudentId());
        record.setRuleId(request.getRuleId());
        record.setCheckInTime(request.getCheckInTime());
        record.setCheckInType("manual");
        record.setStatus("normal");
        record.setRemark(request.getRemark());
        
        attendanceRecordMapper.insert(record);
        return recordId;
    }
    
    @Override
    public Map<String, Object> faceRecognitionCheckIn(FaceRecognitionCheckInRequest request) {
        // 这里应该调用人脸识别服务
        // 为了演示，返回模拟结果
        String recordId = "rec_" + UUID.randomUUID().toString().substring(0, 8);
        
        // 通过Feign客户端获取学生信息
        StudentInfoDTO studentInfo = studentInfoService.getStudentInfo("s002");
        String studentName = studentInfo != null ? studentInfo.getName() : "未知学生";
        
        Map<String, Object> result = new HashMap<>();
        result.put("recordId", recordId);
        result.put("studentId", "s002");
        result.put("studentName", studentName);
        result.put("checkInTime", request.getTimestamp());
        
        return result;
    }
    
    @Override
    public Map<String, Object> getClassAttendanceReport(String classId, LocalDate startDate, LocalDate endDate) {
        // 通过Feign客户端获取班级的学生ID列表
        List<String> studentIds = studentInfoService.getStudentIdsByClassId(classId);
        
        if (studentIds.isEmpty()) {
            Map<String, Object> result = new HashMap<>();
            result.put("classId", classId);
            result.put("statistics", new HashMap<>());
            return result;
        }
        
        // 将学生ID列表转换为SQL IN子句格式
        String studentIdsStr = studentIds.stream()
                .map(id -> "'" + id + "'")
                .collect(Collectors.joining(","));
        
        Map<String, Object> statistics = attendanceRecordMapper.getClassAttendanceStatisticsByStudentIds(
                studentIdsStr, startDate, endDate);
        
        Map<String, Object> result = new HashMap<>();
        result.put("classId", classId);
        result.put("statistics", statistics);
        
        return result;
    }
    
    @Override
    public List<AttendanceRecord> getStudentAttendanceRecords(String studentId, String month) {
        YearMonth yearMonth = YearMonth.parse(month);
        LocalDate startDate = yearMonth.atDay(1);
        LocalDate endDate = yearMonth.atEndOfMonth();
        
        // 获取考勤记录
        List<AttendanceRecord> records = attendanceRecordMapper.getStudentAttendanceRecords(studentId, startDate, endDate);
        
        // 获取学生信息并填充到记录中
        if (!records.isEmpty()) {
            StudentInfoDTO studentInfo = studentInfoService.getStudentInfo(studentId);
            if (studentInfo != null) {
                // 这里可以扩展AttendanceRecord实体类添加studentName字段
                // 或者返回包含学生信息的DTO对象
            }
        }
        
        return records;
    }
}